package com.snail.snailvr.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.snail.snailvr.R;
import com.snail.snailvr.SnailVrService;
import com.snail.snailvr.network.PostService;
import com.snail.snailvr.player.Definition;
import com.snail.snailvr.player.PlayActivity;
import com.snail.snailvr.views.FeedbackActivity;
import com.snail.snailvr.views.HistoryActivity;
import com.snail.snailvr.views.LocalVideosActivity;
import com.snail.snailvr.views.PlayHintActivity;
import com.snail.snailvr.views.SettingActivity;
import com.snail.snailvr.views.WiFiActivity;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by snail on 16/3/28.
 * 界面跳转的集合类
 */
public class IntentUtils {

    /**
     * 打开视频详情界面
     * @param context context
     */
    public static void startAllVideoListActivity(Context context) {
        Intent intent=new Intent(context, LocalVideosActivity.class);
        startActivity(context,intent);
    }
    /**
     * 打开设置界面
     * @param context context
     */
    public static void startSettingActivity(Context context) {
        Intent intent=new Intent(context, SettingActivity.class);
        startActivity(context,intent);
    }
    /**
     * 打开播放历史界面
     * @param context context
     */
    public static void startHistoryActivity(Context context) {
        Intent intent=new Intent(context, HistoryActivity.class);
        startActivity(context,intent);
    }


    public static void playVideo(Context context,String path,String name) {
        Intent intent = PlayHintActivity.getIntent(context, path, name);
        startActivity(context,intent);
    }

    public static void playRealVideo(final Activity context, String path, String name, boolean isNormal){

        if(path!=null&&((Extensions.isVideo(path))||Util.isLiveUrl(path))) {
            path=FileUtil.getRealFilePath(path);
            Intent intent = new Intent(context, PlayActivity.class);
            intent.putExtra(Definition.KEY_PLAY_URL, path);
            intent.putExtra(Constant.KEY_VIDEO_NAME, name);
            intent.putExtra(Constant.KEY_IS_NORMAL, isNormal);
            startActivity(context, intent);
            context.finish();
        }else{
            ToastUtils.showToast(R.string.invalid_video);
            Observable.timer(2, TimeUnit.SECONDS).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
                @Override
                public void call(Long aLong) {
                    context.finish();
                }
            });
        }
    }

    /**
     * 关闭WIFI传输服务
     * @param context
     */
    public static void stopWifiTransferServer(Context context) {
        Intent intent=new Intent(context, SnailVrService.class);
        intent.putExtra(Constant.KEY_BACKGROUND_TASK_TYPE,Constant.STOP_WIFI_TRANS_SERVER);
        context.startService(intent);
    }

    /**
     * 开启WIFI传输服务
     * @param context
     */
    public static void startWifiTransferServer(Context context) {
        Intent intent=new Intent(context, SnailVrService.class);
        intent.putExtra(Constant.KEY_BACKGROUND_TASK_TYPE,Constant.START_WIFI_TRANS_SERVER);
        context.startService(intent);
    }
    /**
     * 打来Wi-Fi传输界面
     * @param activity
     */
    public static void startWifiTransfer(Activity activity) {
        Intent intent = WiFiActivity.getIntent(activity);
        startActivity(activity,intent);
    }

    /**
     * 启动Service监听apk下载
     * @param activity
     */
    public static void startUpdateService(Activity activity) {
        Intent intent = new Intent(activity, SnailVrService.class);
        intent.putExtra(Constant.KEY_BACKGROUND_TASK_TYPE,Constant.START_SLIENT_UPDATE);
        activity.startService(intent);
    }

    /**
     * 问题反馈
     * @param activity
     */
    public static void feedBack(Activity activity) {
        Intent intent = new Intent(activity, FeedbackActivity.class);
        startActivity(activity,intent);
    }

    private static void startActivity(Context context,Intent intent){
        context.startActivity(intent);
        //TODO 添加跳转动画
    }

    public static void feedBackPost(Activity activity, int score, ArrayList<String> pathList, String content, String email) {
        Intent intent = new Intent(activity, PostService.class);
        intent.putStringArrayListExtra(Constant.KEY_MULTI_PIC, pathList);
        intent.putExtra(Constant.KEY_SCORE, score);
        intent.putExtra(Constant.KEY_FEEDBACK_CONTENT, content);
        intent.putExtra(Constant.KEY_FEEDBACK_EMAIL, email);
        activity.startService(intent);
    }

}
